package com.xue566.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xue566.entity.UserEntity;
import com.xue566.service.IUserService;
import com.xue566.util.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private IUserService iUserService;

    @RequestMapping(value = "/queryList", consumes = "application/json")
    public ResultBody<UserEntity> queryList (@RequestBody UserEntity userEntity) {
        Page<UserEntity> page = new Page(userEntity.getPageNum(), userEntity.getPageSize());
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        IPage<UserEntity> pageResult = iUserService.getBaseMapper().selectPage(page, queryWrapper);
        ResultBody<UserEntity> userEntityList = ResultBody.success(userEntity.getPageNum(), userEntity.getPageSize(), pageResult.getTotal(), pageResult.getRecords(), "/userList");
        log.info("{} - {}", userEntity.getTrackId(), userEntityList);
        return userEntityList;
    }


    @RequestMapping(value = "selectUser", consumes = "application/json")
    public ResultBody<UserEntity> selectUser (@RequestBody UserEntity userEntity) {
        log.info("selectUser: {} - {}", userEntity.getTrackId(), userEntity);
        return iUserService.selectUser(userEntity);
    }

}
